/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Banner;
import com.guoyu.project.model.College_Company;
import com.guoyu.project.model.Project_Manage;
import com.guoyu.project.service.College_CompanyService;
import com.guoyu.project.service.FileUploadService;


@Controller
@RequestMapping("admin/collegeCompany")
public class College_CompanyController {

    private final static Logger logger = LoggerFactory.getLogger(College_CompanyController.class);

    @Resource
    private College_CompanyService college_CompanyService;

    @Resource
    private FileUploadService fileUploadService;

    @RequestMapping("index")
    public String index() {
        return "admin/collegeCompany/index";
    }

    /**
     * 信息列表页
     *
     * @return
     */
    @RequestMapping("cclist")
    @ResponseBody
    public Pagination<College_Company> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        return college_CompanyService.queryAllCollege_Company(offset, limit, params);
    }

    /**
     * 跳到添加页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        return "admin/collegeCompany/add";
    }

    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "file", required = false) MultipartFile picture,College_Company college_company) {
        logger.info("提交添加校企信息：{}", JsonUtil.encode(college_company));
        String filePath = fileUploadService.upload(picture);
        college_company.setPicture(filePath);
        college_CompanyService.saveCollege_Company(college_company);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改banner信息信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(Integer college_id, Model model) {
        College_Company college_company = college_CompanyService.queryById(college_id);
        model.addAttribute("college_company", college_company);
        return "admin/collegeCompany/edit";
    }

    /**
     * 更新信息
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "ccPic", required = false) MultipartFile picture,
    		@RequestParam(value = "ccUpdateFil111", required = false)MultipartFile fiFile, 
    		@RequestParam(value = "ccUpdateFil211", required = false)MultipartFile seFile, 
    		@RequestParam(value = "ccUpdateFil311", required = false)MultipartFile thFile, 
    		College_Company college_company) {
        logger.info("提交修改校企信息：{}", JsonUtil.encode(college_company));
        String picPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), college_company.getPicture());
        college_company.setPicture(picPath);
      //文件上传
        String fifilePath=fileUploadService.upload(fiFile);
        college_company.setFiFile(fifilePath);
        
        String sefilePath=fileUploadService.upload(seFile);
        college_company.setSeFile(sefilePath);
        
        String thfilePath=fileUploadService.upload(thFile);
        college_company.setThFile(thfilePath);
        college_CompanyService.updateCollege_Company(college_company);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(Integer college_id) {
    	college_CompanyService.deleteById(college_id);
        return APIResponse.returnSuccess();
    }

}